﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace weighing
{
    internal class GlobalData
    {

        public static AppConfig Config { get; set; }

        public static bool NotifyIconIsShow { get; set; }

        public static void Init()
        {
            if (File.Exists(AppConfig.SavePath))
            {
                try
                {
                    var json = File.ReadAllText(AppConfig.SavePath);
                    Config = (string.IsNullOrEmpty(json) ? new AppConfig() : JsonConvert.DeserializeObject<AppConfig>(json)) ?? new AppConfig();
                }
                catch
                {
                    Config = new AppConfig();
                }
            }
            else
            {
                Config = new AppConfig();
            }
        }

        public static void Save()
        {
            var json = JsonConvert.SerializeObject(Config);
            File.WriteAllText(AppConfig.SavePath, json);
        }

        #region  Backup field Item Source Data

        private static Dictionary<string, List<string>>? _itemSource;

        /// <summary>
        /// 加载 Item Source Data
        /// </summary>
        /// <param name="reLoad">是否重先从文件中加载</param>
        /// <returns></returns>
        public static Dictionary<string, List<string>> GetAllItemSource(bool reLoad = false)
        {
            if (reLoad)
            {
                _itemSource = BackupItemsHelper.Instance.LoadFromFile();
                return _itemSource;
            }
            else
            {
                _itemSource ??= BackupItemsHelper.Instance.LoadFromFile();
                return _itemSource;
            }
        }

        #endregion
    }
}
